package com.archstoday.activity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.common.AsRes;
import com.archstoday.entity.ShareEntity;
import com.archstoday.entity.UserInfoEntity;
import com.archstoday.utils.AppUtils;
import com.archstoday.utils.CodeUtils;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ProcessUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.utils.WechatUtils;
import com.base.http.common.BaseHttpClient;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class TextShareActivity extends AsBaseActivity {
    private TextView mTitleTv;
    private TextView mShareSession;
    private TextView mShareTimeline;
    private TextView mSharelink;
    private TextView mShareCancel;
    private TextView muserName;
    private TextView mContent;
    private LinearLayout llShareContainer;

    private CircleImageView headerImageView;
    private ImageView qrCodeImageView;
    private ImageView contentCodeImageView;
    private ScrollView mShotScreenScrollView;

    private int articleID;
    private String mType;
    private boolean isNews = false;
    private String mChannel;
    private String content;
    ShareEntity entity;

    private Bitmap shareBitmap;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_text_share);

        llShareContainer = (LinearLayout) findViewById(R.id.id_for_share_container);
        mShotScreenScrollView = (ScrollView) findViewById(R.id.id_for_share_scroll_container);

        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mShareSession = (TextView) findViewById(R.id.share_wx_btn);
        mSharelink = (TextView) findViewById(R.id.share_link_btn);
        mShareTimeline = (TextView) findViewById(R.id.share_group_btn);
        mShareCancel = (TextView) findViewById(R.id.share_cancel_btn);

        muserName = (TextView) findViewById(R.id.id_for_user_name);
        mContent = (TextView) findViewById(R.id.id_for_share_content);
        headerImageView = (CircleImageView) findViewById(R.id.user_head_icon);
        contentCodeImageView = (ImageView) findViewById(R.id.id_for_background);
        qrCodeImageView = (ImageView) findViewById(R.id.id_for_qr_code);

//        mImageshow = (ImageView) findViewById(R.id.id_for_image_show);
    }

    @Override
    protected void initData(Bundle bundle) {
        articleID = getIntent().getIntExtra("id", 0);
        content = getIntent().getStringExtra("content");
        mType = getIntent().getStringExtra("type");

        mTitleTv.setText("分享");
        mContent.setText(ProcessUtils.procesText(content));

        UserInfoEntity.DataBean userInfo = OAuthUtils.getInstance().getUserInfo();
        if (userInfo != null) {
            String header = userInfo.getUserImg();
            Glide.with(mContext)
                    .load(header)
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>(AppUtils.dip2px(mContext, 25), AppUtils.dip2px(mContext, 25)) {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            Drawable drawable = new BitmapDrawable(resource);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                headerImageView.setBackground(drawable);
                            }
                        }
                    });

            muserName.setText(ProcessUtils.procesText(userInfo.getMobileNo()));
        }

        share();

        super.initData(bundle);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;

            case R.id.share_wx_btn:
                Map<String, String> info = new HashMap<>();
                info.put("articleId", String.valueOf(articleID));
                info.put("categoryId1", String.valueOf(getIntent().getIntExtra("categoryId1", 0)));
                info.put("channel", "wechat");
                getEvent(BaseApp.E_TEXT_SHARE, BaseApp.PAGE_TYPE_TEXT_SHARE,
                        ToolUtils.mapToJson(info));

                WechatUtils.shareBigThumb(mContext, layout2Bitmap(llShareContainer), 1);
                break;

            case R.id.share_group_btn:
                info = new HashMap<>();
                info.put("articleId", String.valueOf(articleID));
                info.put("categoryId1", String.valueOf(getIntent().getIntExtra("categoryId1", 0)));
                info.put("channel", "friends");
                getEvent(BaseApp.E_TEXT_SHARE, BaseApp.PAGE_TYPE_TEXT_SHARE,
                        ToolUtils.mapToJson(info));

                WechatUtils.shareBigThumb(mContext, layout2Bitmap(llShareContainer), 2);
                break;

            case R.id.share_link_btn:
                info = new HashMap<>();
                info.put("articleId", String.valueOf(articleID));
                info.put("categoryId1", String.valueOf(getIntent().getIntExtra("categoryId1", 0)));
                info.put("channel", "link");
                getEvent(BaseApp.E_TEXT_SHARE, BaseApp.PAGE_TYPE_TEXT_SHARE,
                        ToolUtils.mapToJson(info));

                if (entity == null) {
                    return;
                }

                ToolUtils.copyUrl(mContext, entity.getData().getUrl());
                break;

            case R.id.share_cancel_btn:

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.share_wx_btn);
        setOnClickListener(R.id.share_link_btn);
        setOnClickListener(R.id.share_group_btn);
        setOnClickListener(R.id.share_cancel_btn);
    }

    private void share() {
        initParameter();
        mParams.put("articleID", articleID);
        mParams.put("type", mType);
        mParams.put("channel", "wechat");

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.SHARE),
                AsRes.SHARE, mParams, ShareEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        Log.e("onSuccess", "onSuccess");

        if (client.getUrlIdentifier() == AsRes.SHARE) {
            entity = (ShareEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                getEvent(mApp.E_SHARE, mApp.PAGE_TYPE_SHARE);

                Glide.with(mContext)
                        .load(entity.getData().getImgurl())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>(640, 180) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Drawable drawable = new BitmapDrawable(resource);
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                    contentCodeImageView.setBackground(drawable);
                                }
                            }
                        });

                String mShareUrl = entity.getData().getUrl();

                Bitmap bitmap = CodeUtils.createQRImage(mShareUrl,
                        (int) mContext.getResources().getDimension(R.dimen.dp60),
                        (int) mContext.getResources().getDimension(R.dimen.dp60));

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] bytes = baos.toByteArray();

                Glide.with(mContext)
                        .load(bytes)
                        .asBitmap()
                        .placeholder(R.mipmap.icon_backgroud)
                        .into(new SimpleTarget<Bitmap>(100, 100) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Drawable drawable = new BitmapDrawable(resource);
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                    qrCodeImageView.setBackground(drawable);
                                }
                            }
                        });
            } else {
                showToast(entity.getMsg());
            }
        }
    }

    public Bitmap layout2Bitmap(LinearLayout scrollView) {
        int width = scrollView.getMeasuredWidth();
        int height = scrollView.getMeasuredHeight();
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(b);
        scrollView.draw(canvas);

        return b;
    }
}
